package cn.ds.assets.module.assets.controller.admin.projectMember;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.projectMember.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.projectMember.BaseProjectMemberDO;
import cn.ds.assets.module.assets.convert.projectMember.BaseProjectMemberConvert;
import cn.ds.assets.module.assets.service.projectMember.BaseProjectMemberService;

//@Tag(name = "管理后台 - 项目成员")
//@RestController
//@RequestMapping("/assets/base-project-member")
@Validated
public class BaseProjectMemberController {

    @Resource
    private BaseProjectMemberService baseProjectMemberService;

    @PostMapping("/create")
    @Operation(summary = "创建项目成员")
    @PreAuthorize("@ss.hasPermission('assets:base-project-member:create')")
    public CommonResult<Long> createBaseProjectMember(@Valid @RequestBody BaseProjectMemberCreateReqVO createReqVO) {
        return success(baseProjectMemberService.createBaseProjectMember(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目成员")
    @PreAuthorize("@ss.hasPermission('assets:base-project-member:update')")
    public CommonResult<Boolean> updateBaseProjectMember(@Valid @RequestBody BaseProjectMemberUpdateReqVO updateReqVO) {
        baseProjectMemberService.updateBaseProjectMember(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目成员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-project-member:delete')")
    public CommonResult<Boolean> deleteBaseProjectMember(@RequestParam("id") Long id) {
        baseProjectMemberService.deleteBaseProjectMember(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目成员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-project-member:query')")
    public CommonResult<BaseProjectMemberRespVO> getBaseProjectMember(@RequestParam("id") Long id) {
        BaseProjectMemberDO baseProjectMember = baseProjectMemberService.getBaseProjectMember(id);
        return success(BaseProjectMemberConvert.INSTANCE.convert(baseProjectMember));
    }

    @GetMapping("/list")
    @Operation(summary = "获得项目成员列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-project-member:query')")
    public CommonResult<List<BaseProjectMemberRespVO>> getBaseProjectMemberList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseProjectMemberDO> list = baseProjectMemberService.getBaseProjectMemberList(ids);
        return success(BaseProjectMemberConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目成员分页")
    @PreAuthorize("@ss.hasPermission('assets:base-project-member:query')")
    public CommonResult<PageResult<BaseProjectMemberRespVO>> getBaseProjectMemberPage(@Valid BaseProjectMemberPageReqVO pageVO) {
        PageResult<BaseProjectMemberDO> pageResult = baseProjectMemberService.getBaseProjectMemberPage(pageVO);
        return success(BaseProjectMemberConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目成员 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-project-member:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseProjectMemberExcel(@Valid BaseProjectMemberExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseProjectMemberDO> list = baseProjectMemberService.getBaseProjectMemberList(exportReqVO);
        // 导出 Excel
        List<BaseProjectMemberExcelVO> datas = BaseProjectMemberConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目成员.xls", "数据", BaseProjectMemberExcelVO.class, datas);
    }

}
